package com.tfxy.hxl.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class JacksonConfig {
    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        
        // 创建JavaTimeModule
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        
        // 配置LocalDateTime的序列化和反序列化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
        
        // 使用自定义的反序列化器来处理多种日期格式
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter) {
            @Override
            public LocalDateTime deserialize(com.fasterxml.jackson.core.JsonParser p, com.fasterxml.jackson.databind.DeserializationContext ctxt) throws java.io.IOException {
                String text = p.getText();
                if (text == null || text.isEmpty()) {
                    return null;
                }
                
                try {
                    // 处理ISO格式 (2025-07-22T00:00:00.000Z)
                    if (text.contains("T") && text.contains("Z")) {
                        // 移除Z和毫秒部分
                        String cleanText = text.replace("Z", "").replace(".000", "");
                        return LocalDateTime.parse(cleanText, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
                    }
                    
                    // 处理ISO格式 (2025-07-22T00:00:00)
                    if (text.contains("T")) {
                        return LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
                    }
                    
                    // 处理标准格式 (2025-07-22 00:00:00)
                    return LocalDateTime.parse(text, dateTimeFormatter);
                    
                } catch (Exception e) {
                    // 如果所有格式都失败，尝试更宽松的解析
                    try {
                        // 尝试解析不完整的格式
                        if (text.matches("\\d{4}-\\d{2}-\\d{2}")) {
                            return LocalDateTime.parse(text + " 00:00:00", dateTimeFormatter);
                        }
                        if (text.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}")) {
                            return LocalDateTime.parse(text + ":00", dateTimeFormatter);
                        }
                    } catch (Exception e2) {
                        // 如果还是失败，返回当前时间
                        System.err.println("无法解析日期时间: " + text + ", 使用当前时间");
                        return LocalDateTime.now();
                    }
                    throw new RuntimeException("无法解析日期时间: " + text, e);
                }
            }
        });
        
        // 注册模块
        objectMapper.registerModule(javaTimeModule);
        
        // 禁用将日期写为时间戳
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        
        return objectMapper;
    }
} 